package com.qiankun.tang.ui.view.adapter.home;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.net.items.BaseItem;
import com.common.library.ui.view.ClickEffectLinearLayout;
import com.common.library.ui.view.LinearSpacingItemDecoration;
import com.common.library.ui.view.adapter.base.BaseDrawRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.HomeItem;
import com.qiankun.tang.ui.view.ADWidget;
import com.qiankun.tang.ui.view.VideoSynopsisListWidget;
import com.qiankun.tang.ui.view.adapter.holder.BaseLineHolder;

import java.util.List;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter
 * @date 2017/6/9 0009
 */

public class HomeAdapter extends BaseDrawRecyclerAdapter<HomeItem> {
    public HomeAdapter(Context context) {
        super(context);
    }
    public HomeAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }

    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HomeItem.TYPE_0:
                return new HomeViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_home_0, parent, false));
            case HomeItem.TYPE_1:
                return new HomeViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_home_1, parent, false));
            case HomeItem.TYPE_2:
                return new HomeViewHolder2(LayoutInflater.from(getContext()).inflate(R.layout.item_home_2, parent, false));
            case BaseItem.TYPE_LINE:
                return new BaseLineHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_line, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HomeViewHolder0) {
            onBindHomeViewHolder0((HomeViewHolder0) holder, position);
        } else if (holder instanceof HomeViewHolder1) {
            onBindHomeViewHolder1((HomeViewHolder1) holder, position);
        } else if (holder instanceof HomeViewHolder2) {
            onBindHomeViewHolder2((HomeViewHolder2) holder, position);
        }
    }

    private void onBindHomeViewHolder2(HomeViewHolder2 holder, int position) {
        final List<VideoSynopsisBean> beans = (List<VideoSynopsisBean>) getDatas().get(position).getBean();
        holder.videoSynopsisListWidget.setData(beans);
        holder.videoSynopsisListWidget.addOnItemClickListener(new VideoSynopsisListWidget.OnItemClickListener() {
            @Override
            public void onItemClick(int type, VideoSynopsisBean bean) {
                getListener().onInteraction(HomeItem.TYPE_2, bean);
            }
        });
    }

    private void onBindHomeViewHolder1(HomeViewHolder1 holder, int position) {
        final VideoSynopsisBean bean = (VideoSynopsisBean) getDatas().get(position).getBean();
        holder.mImge.setImageURI(bean.getImgUrl());
        holder.mTitle.setText(bean.getTitle());
        holder.mSubTitle.setText(bean.getSubtitle());

        holder.clickEffectLinearLayout.addOnClickListener(new ClickEffectLinearLayout.OnClickListener() {
            @Override
            public void onClick() {
                getListener().onInteraction(HomeItem.TYPE_1, bean);
            }
        });
    }

    private void onBindHomeViewHolder0(HomeViewHolder0 holder, int position) {
        List<ADBean> datas = (List<ADBean>) getDatas().get(position).getBean();
        holder.ADWidget.setData(datas, 0);
        holder.ADWidget.addOnClickListaner(new ADWidget.OnClickListaner() {
            @Override
            public void onItemClick(ADBean bean) {
                getListener().onInteraction(HomeItem.TYPE_0, bean);
            }

            @Override
            public void onViewClick() {
                getListener().onInteraction(HomeItem.TYPE_0, 0);
            }
        });
        if (getOnDrawTouchClick() != null)
            getOnDrawTouchClick().onDrawTouch(holder.ADWidget);

    }

    class HomeViewHolder0 extends RecyclerView.ViewHolder {
        ADWidget ADWidget;

        public HomeViewHolder0(View itemView) {
            super(itemView);
            ADWidget = (ADWidget) itemView.findViewById(R.id.item_home_ADWidget);
        }
    }

    class HomeViewHolder1 extends RecyclerView.ViewHolder {
        SimpleDraweeView mImge;
        TextView mTitle, mSubTitle;
        ClickEffectLinearLayout clickEffectLinearLayout;

        public HomeViewHolder1(View itemView) {
            super(itemView);
            clickEffectLinearLayout = (ClickEffectLinearLayout) itemView.findViewById(R.id.item_home_1_clickEffectLinearLayout);
            mImge = (SimpleDraweeView) itemView.findViewById(R.id.item_home_1_img);
            mTitle = (TextView) itemView.findViewById(R.id.item_home_1_title);
            mSubTitle = (TextView) itemView.findViewById(R.id.item_home_1_subtitle);
        }
    }

    class HomeViewHolder2 extends RecyclerView.ViewHolder {
        VideoSynopsisListWidget videoSynopsisListWidget;

        public HomeViewHolder2(View itemView) {
            super(itemView);
            videoSynopsisListWidget = (VideoSynopsisListWidget) itemView.findViewById(R.id.item_home_2_VideoSynopsisLisetWidget);
            videoSynopsisListWidget.setItemDecoration(new LinearSpacingItemDecoration(LinearLayoutManager.HORIZONTAL, dip2px(4), true));
        }
    }
}
